import { Store } from "../../core";

import GoToLastPageButton from "./GoToLastPageButton";
import StoreProps from "./StoreProps";
import { RenderGoToPage, RenderGoToPageProps } from "./types/index";
import useCurrentPage from "./useCurrentPage";
import useNumberOfPages from "./useNumberOfPages";
import { FC } from "react";

const GoToLastPage: FC<{
  children?: RenderGoToPage;
  store: Store<StoreProps>;
}> = ({ children, store }) => {
  const { currentPage } = useCurrentPage(store);
  const { numberOfPages } = useNumberOfPages(store);

  const goToLastPage = () => {
    const jumpToPage = store.get("jumpToPage");
    if (jumpToPage) {
      jumpToPage(numberOfPages - 1);
    }
  };

  const defaultChildren = (props: RenderGoToPageProps) => (
    <GoToLastPageButton isDisabled={props.isDisabled} onClick={props.onClick} />
  );
  const render = children || defaultChildren;

  return render({
    isDisabled: currentPage + 1 >= numberOfPages,
    onClick: goToLastPage,
  });
};

export default GoToLastPage;
